<template>
  <div>
    <div class="trading">
      <div id="china-map"></div>
      <div class="trading-name" v-for="item in maplist" :key="item.name">
        <span>{{ item.name }}</span>
      </div>
    </div>
    <div>
      <Footer></Footer>
    </div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import chinaJson from "../api/china.js";
import {getmap} from "../api/api.js";

export default {
  name: "Trading",
  data() {
    return {
      chart: null,
      maplist:[],
    };
  },
  mounted() {
    this.$nextTick(() => {
      echarts.registerMap("china", chinaJson);
      this.initMap();
      // 添加点击事件监听
      this.chart.on("click", this.handleMapClick);
    });
    getmap().then((res) => {
      console.log(res.data);
      this.maplist = res.data;
      console.log(this.maplist);
      this.initMap();
    });
  },
  methods: {
    initMap() {
      this.chart = echarts.init(document.getElementById("china-map"));
      const option = {
        title: {
          left: "center",
        },
        tooltip: {
          trigger: "item",
          formatter: "{b}",
        },
        series: [
          {
            name: "中国",
            type: "map",
            map: "china",
            roam: true,
            center: [105, 36],
            zoom: 1.5,
            label: {
              show: false,
            },
            markPoint: {
              symbol: "triangle",
              symbolSize: [15, 15],
              label: {
                position: "top",
                color: "#1e1e1e",
              },
              data: this.maplist,
            },
          },
        ],
      };
      this.chart.setOption(option);
    },

    // 添加点击事件处理函数
    handleMapClick(params) {
      console.log("点击了地图：", params);
      if(params.data==undefined){
        return
      }
      window.open(params.data.guanWang);
    },
  },
  beforeDestroy() {
    if (this.chart) {
      // 移除事件监听
      this.chart.off("click");
      this.chart.dispose();
    }
  },
};
</script>

<style lang="less" scoped>
.trading {
  display: flex;
  flex-direction: row;
  width: 100%;

  .trading-name {
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    // margin-left: 20px;
    // min-width: 200px;

    span {
      padding: 10px 20px;
      margin: 10px 0;
      background-color: #f5f5f5;
      border-radius: 6px;
      color: #333;
      font-size: 16px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      cursor: pointer;
      transition: all 0.3s ease;

      &:hover {
        background-color: #e8e8e8;
        transform: translateY(-2px);
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
      }
    }
  }
}

#china-map {
  width: 1000px;
  height: 830px;
}
</style>